package main

import (
	"k8shomework/04_homework/metrics"
	"net/http"
	"time"

	"math/rand"

	"github.com/gin-gonic/gin"
	"github.com/prometheus/client_golang/prometheus/promhttp"
)

func randInt(min int, max int) int {
	rand.Seed(time.Now().UTC().UnixNano())
	return min + rand.Intn(max-min)
}

func main() {

	// metrics.Register()

	// 1.创建路由
	r := gin.Default()
	// 2.绑定路由规则，执行的函数
	r.GET("/", func(c *gin.Context) {
		// 延迟
		timer := metrics.NewTimer()
		defer timer.ObserveTotal()
		delay := randInt(10, 2000)
		time.Sleep(time.Millisecond * time.Duration(delay))

		c.String(http.StatusOK, "hello World!")
	})

	// 调用promhttp.Handler()，但是gin没法直接使用go 中http
	r.GET("/metrics", gin.WrapH(promhttp.Handler()))

	r.GET("/healthz", func(c *gin.Context) {
		c.String(http.StatusOK, "healthz")
	})
	// 3.监听端口，默认在8080
	// Run("里面不指定端口号默认为8080")
	r.Run()
}

